using System;
using System.ComponentModel;
using System.Net;
using System.Net.NetworkInformation;
using System.Collections.Generic; // Added for List<>
using System.Linq; // Added for LINQ extensions

namespace DeviceConfigTool
{
    public class DeviceInfo : INotifyPropertyChanged
    {
        private string _ipAddress;
        private string _macAddress;
        private string _subnetMask;
        private string _gateway;
        private string _serialNumber;
        private string _softwareVersion;
        private bool _useDhcp;
        private string _cloudServerIp;
        private string _deviceType;
        private bool _clearArp;

        public event PropertyChangedEventHandler PropertyChanged;

        public string IpAddress
        {
            get => _ipAddress;
            set { _ipAddress = value; OnPropertyChanged(nameof(IpAddress)); }
        }

        public string MacAddress
        {
            get => _macAddress;
            set { _macAddress = value; OnPropertyChanged(nameof(MacAddress)); }
        }

        public string SubnetMask
        {
            get => _subnetMask;
            set { _subnetMask = value; OnPropertyChanged(nameof(SubnetMask)); }
        }

        public string Gateway
        {
            get => _gateway;
            set { _gateway = value; OnPropertyChanged(nameof(Gateway)); }
        }

        public string SerialNumber
        {
            get => _serialNumber;
            set { _serialNumber = value; OnPropertyChanged(nameof(SerialNumber)); }
        }

        public string SoftwareVersion
        {
            get => _softwareVersion;
            set { _softwareVersion = value; OnPropertyChanged(nameof(SoftwareVersion)); }
        }

        public bool UseDhcp
        {
            get => _useDhcp;
            set { _useDhcp = value; OnPropertyChanged(nameof(UseDhcp)); }
        }

        public string CloudServerIp
        {
            get => _cloudServerIp;
            set { _cloudServerIp = value; OnPropertyChanged(nameof(CloudServerIp)); }
        }

        public string DeviceType
        {
            get => _deviceType;
            set { _deviceType = value; OnPropertyChanged(nameof(DeviceType)); }
        }

        public bool ClearArp
        {
            get => _clearArp;
            set { _clearArp = value; OnPropertyChanged(nameof(ClearArp)); }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public byte[] ToByteArray(bool restart, bool factoryReset, bool clearArp, string cloudServerIp)
        {
            var packets = new List<byte>();

            // MAC address (0x01)
            if (!string.IsNullOrEmpty(MacAddress))
            {
                packets.Add(0x01); // Code
                packets.Add(6);    // Length
                packets.AddRange(MacAddress.Split(':').Select(x => Convert.ToByte(x, 16)));
            }

            // DHCP/Static IP (0x02)
            packets.Add(0x02); // Code
            packets.Add(1);    // Length
            packets.Add(UseDhcp ? (byte)1 : (byte)0);

            // Static IP (0x03)
            if (!UseDhcp && !string.IsNullOrEmpty(IpAddress))
            {
                packets.Add(0x03); // Code
                packets.Add(4);    // Length
                packets.AddRange(IpAddress.Split('.').Select(x => byte.Parse(x)));
            }

            // Subnet mask (0x04)
            if (!UseDhcp && !string.IsNullOrEmpty(SubnetMask))
            {
                packets.Add(0x04); // Code
                packets.Add(4);    // Length
                packets.AddRange(SubnetMask.Split('.').Select(x => byte.Parse(x)));
            }

            // Gateway (0x05)
            if (!UseDhcp && !string.IsNullOrEmpty(Gateway))
            {
                packets.Add(0x05); // Code
                packets.Add(4);    // Length
                packets.AddRange(Gateway.Split('.').Select(x => byte.Parse(x)));
            }

            // Device operations (0x06)
            if (restart || factoryReset)
            {
                packets.Add(0x06); // Code
                packets.Add(1);    // Length
                packets.Add(restart ? (byte)0x55 : (byte)0xAA);
            }

            // Clear ARP (0x07)
            if (clearArp)
            {
                packets.Add(0x07); // Code
                packets.Add(0);    // Length (no data)
            }

            // Cloud server IP (0x08)
            if (!string.IsNullOrEmpty(cloudServerIp))
            {
                packets.Add(0x08); // Code
                packets.Add(4);    // Length
                packets.AddRange(cloudServerIp.Split('.').Select(x => byte.Parse(x)));
            }

            // Serial number (0x09)
            if (!string.IsNullOrEmpty(SerialNumber))
            {
                var serialBytes = System.Text.Encoding.ASCII.GetBytes(SerialNumber);
                packets.Add(0x09); // Code
                packets.Add((byte)serialBytes.Length); // Length
                packets.AddRange(serialBytes);
            }

            // Software version (0x0A)
            if (!string.IsNullOrEmpty(SoftwareVersion))
            {
                var versionBytes = System.Text.Encoding.ASCII.GetBytes(SoftwareVersion);
                packets.Add(0x0A); // Code
                packets.Add((byte)versionBytes.Length); // Length
                packets.AddRange(versionBytes);
            }

            return packets.ToArray();
        }

        public static DeviceInfo FromByteArray(byte[] data)
        {
            // Deserialize device info from UDP response
            // Implementation depends on protocol with devices
            return new DeviceInfo(); // Placeholder
        }
    }
}
